//
//  ViewController.m
//  MetenEnglish
//
//  Created by rj-mayong on 14/11/17.
//  Copyright (c) 2014年 Shenzhen Securities Information Co., Ltd. All rights reserved.
//

#import "ViewController.h"
#import "NetWork.h"
#import "ASIFormDataRequest.h"
#import "OCGumbo+Query.h"
#import "CourseModel.h"
#import "ZCActionOnCalendar.h"
#import "CourseListController.h"
#import "ResevedListController.h"

#import "HttpManager.h"
#import "SVProgressHUD.h"

#import <EventKit/EventKit.h>

@interface ViewController () {
    NSString *_checkCode;
    NSArray *_cookies;
    NSArray *_courses;
    UITextField *_userName;
    UITextField *_password;
    UIButton *_saveEventButton;
    UIButton *_reserveButton;
    UIButton *_loginButton;
    NSString *_userLevel;
}

@end

@implementation ViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    _userName = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.view.frame) / 2 - 100,
                                                                          154,
                                                                          200,
                                                                          30)];
    _userName.borderStyle = UITextBorderStyleBezel;
    _userName.placeholder = @"请输入用户名";
    _userName.font = [UIFont systemFontOfSize:16];
    _userName.delegate = self;
    _userName.accessibilityLabel = @"userName";
    [self.view addSubview:_userName];
    
    
    _password = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.view.frame) / 2 - 100,
                                                              CGRectGetMaxY(_userName.frame) + 30 + 20,
                                                                          200,
                                                                          30)];
    _password.borderStyle = UITextBorderStyleBezel;
    _password.secureTextEntry = YES;
    _password.placeholder = @"请输入密码";
    _password.accessibilityLabel = @"password";
    _password.font = [UIFont systemFontOfSize:16];
    _password.delegate = self;
    [self.view addSubview:_password];
    
    NSString *userNameString = [[NSUserDefaults standardUserDefaults] objectForKey:USERNAME_KEY];
    NSString *passwordString = [[NSUserDefaults standardUserDefaults] objectForKey:PASSWORD_KEY];
    _userName.text = userNameString;
    _password.text = passwordString;
    
    _loginButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    _loginButton.frame = CGRectMake(CGRectGetMaxX(self.view.frame) / 2 - 40,
                                   CGRectGetMaxY(_password.frame) + 30,
                                   80,
                                   30);
    [_loginButton setTitle:@"login" forState:UIControlStateNormal];
    _loginButton.titleLabel.textAlignment = NSTextAlignmentCenter;
    [_loginButton setTitleColor:OVERALL_COLOR forState:UIControlStateNormal];
    [_loginButton addTarget:self action:@selector(getCheckCodeFromURL) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_loginButton];
    
    UILabel *labelTop = [[UILabel alloc] initWithFrame:CGRectMake(0,
                                                               CGRectGetHeight(self.view.frame) - 70,
                                                               CGRectGetWidth(self.view.frame),
                                                               30)];
    labelTop.textAlignment = NSTextAlignmentCenter;
    labelTop.font = [UIFont systemFontOfSize:12];
    labelTop.text = @"警告: 请勿在日历提醒事项的标题中使用 \"&^\" 两个特殊符号";
    [self.view addSubview:labelTop];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0,
                                                               CGRectGetHeight(self.view.frame) - 50,
                                                               CGRectGetWidth(self.view.frame),
                                                               30)];
    label.textAlignment = NSTextAlignmentCenter;
    label.font = [UIFont systemFontOfSize:12];
    label.text = @"否则会导致提示事项被误删";
    [self.view addSubview:label];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)getCheckCodeFromURL {
    // If Username or Password is null, alert users they are required.
    if ([_userName.text isEqualToString:@""] || [_password.text isEqualToString:@""]) {
        [self showAlertView:@"用户名和密码不能为空"];
        return;
    }
    
    // prevent double clicking the login button
    _loginButton.enabled = NO;
    
    // login normally
    [SVProgressHUD showWithStatus:@"正在登录..."];
    [[HttpManager share] loginMetenWithUserName:_userName.text
                                       password:_password.text];
}

- (void)obtainClassMessage {
    [SVProgressHUD showWithStatus:@"正在获取订课记录..."];
    
    [[HttpManager share] loginMetenWithUserName:_userName.text
                                       password:_password.text
                                      checkCode:_checkCode];
}

- (void)showAlertView:(NSString *)message {
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:message
                                                        message:nil
                                                       delegate:self
                                              cancelButtonTitle:@"确定"
                                              otherButtonTitles:nil, nil];
    [alertView show];
}


#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [_userName resignFirstResponder];
    [_password resignFirstResponder];
}

@end
